#!/usr/bin/env  node

//node cli 应用入口文件必须要有这样的文件夹
//Linux 和macos 系统需要用需要修改文件读取权限为755  
//chmod 755 cli.js  

//脚手架工作过程 读取用户输入的参数=> 生成对应的文件
const inquirer = require('inquirer')
const fs = require('fs')
const path = require('path')
const ejs = require('ejs')
inquirer.prompt([{
  type: 'input',
  name: 'name',
  message: 'project name',
  default: "temp-project"
},
{
  type: "list",
  message: "请选择需要的title",
  name: 'title',
  choices: [
    "Apple",
    "orange",
    "Banana"
  ],
  filter: function (val) {
    return val.toLowerCase();
  }
}
]).then(answers => {
  // console.log(answers)
  const tmpDir = path.join(__dirname, "templates")
  const destDir = process.cwd()
  fs.readdir(tmpDir, (err, files) => {
    if (err) throw err
    files.forEach(file => {
      ejs.renderFile(path.join(tmpDir, file), answers, (err, result) => {
        if (err) throw err
        fs.writeFileSync(path.join(destDir, file), result)
      })
    })
  })

})